import { getEmpList } from '@/api/organize'
import { ref } from 'vue'

/**
 * 组织员工列表服务
 */
export default function () {
  const getOrganizeEmpListLoading = ref(false)
  const getOrganizeEmpListParam = ref({
    limit: 50,
    name: '',
    organizeId: '',
    page: 1,
    phone: '',
  })
  const total = ref(0)
  const organizeEmpList = ref<Array<OrganizeAPIResponse.OrganizeEmpListItem>>([])

  const resetGetOrganizeEmpListParam = () => {
    getOrganizeEmpListParam.value = {
      limit: 50,
      name: '',
      organizeId: '',
      page: 1,
      phone: '',
    }
  }

  const getOrganizeEmpList = async () => {
    try {
      getOrganizeEmpListLoading.value = true
      const res = await getEmpList(getOrganizeEmpListParam.value)
      getOrganizeEmpListLoading.value = false
      if (res.data.code === 20000) {
        total.value = Number(res.data.data.total)
        organizeEmpList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getOrganizeEmpListLoading.value = false
      return '获取列表失败，请稍后重试'
    }
  }

  return {
    total,
    organizeEmpList,
    getOrganizeEmpListParam,
    getOrganizeEmpListLoading,
    getOrganizeEmpList,
    resetGetOrganizeEmpListParam,
  }
}
